// src/locales/i18n.ts
import { createI18n } from 'vue-i18n'
import en from './en/common.json'
console.log('Manual import:', en)

// 关键：正确加载语言文件（假设语言文件直接放在 locales 目录下）
// src/locales/i18n.ts
const loadLocaleMessages = () => {
  const messages: Record<string, any> = {}

  const localeFiles = import.meta.glob('./**/*.json', { eager: true })

  for (const path in localeFiles) {
    const pathParts = path.split('/').slice(1)
    const locale = pathParts[0] // 'en' 或 'zh-CN'
    const module = pathParts[1].replace('.json', '') // 'i18nDemo'

    if (!messages[locale]) {
      messages[locale] = {}
    }
    messages[locale][module] = localeFiles[path]
  }

  // 合并模块到语言层级（而非顶层）
  const mergedMessages: Record<string, any> = {}
  for (const locale in messages) {
    mergedMessages[locale] = {}
    for (const module in messages[locale]) {
      Object.assign(mergedMessages[locale], messages[locale][module])
    }
  }

  console.log('Final messages:', mergedMessages)
  return mergedMessages
}

const i18n = createI18n({
  legacy: false,

  locale: 'zh-CN',
  fallbackLocale: 'en',
  messages: loadLocaleMessages(),
  datetimeFormats: {
    en: {
      short: { year: 'numeric', month: 'short', day: 'numeric' }
    },
    'zh-CN': {
      short: { year: 'numeric', month: '2-digit', day: '2-digit' }
    }
  },
  numberFormats: {
    en: {
      currency: { style: 'currency', currency: 'USD' }
    },
    'zh-CN': {
      currency: { style: 'currency', currency: 'CNY' }
    }
  }
}) as any // 暂时忽略类型检查

export default i18n
